﻿<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="admin_Modules_NewsEvent_NewsCategory_Default" %>


<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title></title>
    <link href="/admin/resources/css/base.css" rel="stylesheet" type="text/css" />
    <script src="/admin/resources/js/base.js" type="text/javascript"></script>
    <script type="text/javascript">

        var buildTree = function () {
            var node = treeCategory.getRootNode();

            Ext.net.DirectMethods.NodeLoad({
                success: function (result) {
                    var data = eval("(" + result + ")");
                    node.loadNodes(data);
                },

                failure: function () {
                    onFailure();
                }
            });
        }

        var moveNode = function (tree, node, oldParent, newParent, index) {
            Ext.net.DirectMethods.UpdateNewsCategoryRelationship(node.id.toString(), newParent.id.toString(), index, {

                failure: function () {
                    onFailure();
                }
            });
        }

        var reload = function () {

            //alert('xx');
            var node = treeCategory.getRootNode();

            while (node.firstChild) {
                node.removeChild(node.firstChild);
            }

            Ext.net.DirectMethods.NodeLoad({
                success: function (result) {
                    var data = eval("(" + result + ")");
                    node.loadNodes(data);
                },

                failure: function (errorMsg) {
                    onFailure();
                }
            });

        }

        var ContextMenu = function (node, e) {
            if (node.id == "-1") {
                rootMenu.node = node; node.select(); rootMenu.showAt(e.getXY());
            }

            if (node.id > 0) {
                treeMenu.node = node; node.select(); treeMenu.showAt(e.getXY());
            }
        }

        var getDropPoint = function (e, n, dd) {
            var tn = n.node;
            if (tn.isRoot) {
                return tn.allowChildren !== false ? "append" : false;
            }
            var dragEl = n.ddel;
            var t = Ext.lib.Dom.getY(dragEl), b = t + dragEl.offsetHeight;
            var y = Ext.lib.Event.getPageY(e);
            var noAppend = tn.allowChildren === false;
            if (this.appendOnly || tn.parentNode.allowChildren === false) {
                return noAppend ? false : "append";
            }
            var noBelow = false;
            if (!this.allowParentInsert) {
                noBelow = tn.hasChildNodes() && tn.isExpanded();
            }
            var q = (b - t) / (noAppend ? 2 : 3);
            if (y >= t && y < (t + q)) {
                return "above";
            } else if (!noBelow && (noAppend || y >= b - q && y <= b)) {
                return "below";
            } else {
                return "append";
            }
        };


        function confirm(deleteId, text) {
            isContinue = false;

            Ext.Msg.confirm('Xác nhận', 'Xóa menu:<b style="color:red;"> ' + text + '</b>', function (btn) {
                if (btn == "yes") {
                    isContinue = true;
                    if (isContinue) {
                        Ext.net.DirectMethods.DeleteCategory(deleteId, {
                            success: function () {
                                //Ext.Msg.hide();
                                //Ext.net.Msg.hide();
                                //reload();
                                hideWait();
                            },
                            before: function () {
                                showWait();
                            },
                            failure: function (errorMsg) {
                                onFailure();
                            }
                        });
                    }
                }
            });
        }


        function loadAddOrEditForm(mode, id) {
            var url = "/admin/modules/newsevent/newscategory/addoredit.aspx?mode=" + mode + "&id=" + id + "&cbFX=reload&tId=" + getTabPanelID();
            window.top.createWindow('addoreditcategory', url, 'Tạo mới, chỉnh sửa menu', 680, 460, true);
        }

        function loadOrderForm(id) {
            var url = "/admin/modules/newsevent/newscategory/sort.aspx?id=" + id + "&cbFX=reload&tId=" + getTabPanelID();
            window.top.createWindow('sortCategory', url, 'Sắp xếp menu tin bài', 580, 460, true);
        }

    </script>
</head>
<body>
    <form id="form1" runat="server">
        <ext:ResourceManager ID="SM" runat="server">
            <Listeners>
                <DocumentReady Handler="buildTree();" />
            </Listeners>
        </ext:ResourceManager>
        <ext:Menu runat="server" ID="rootMenu">
            <Items>
                <ext:MenuItem Text="Tạo mới" Icon="Add">
                    <Listeners>
                        <Click Handler="loadAddOrEditForm('add','0');" />
                    </Listeners>
                </ext:MenuItem>
                <ext:MenuSeparator>
                </ext:MenuSeparator>
                <ext:MenuItem Icon="SortAscending" Text="Sắp xếp">
                    <Listeners>
                        <Click Handler="loadOrderForm(0)" />
                    </Listeners>
                </ext:MenuItem>
            </Items>
        </ext:Menu>
        <ext:Menu runat="server" ID="treeMenu">
            <Items>
                <ext:MenuItem Text="Tạo mới" Icon="Add">
                    <Listeners>
                        <Click Handler="loadAddOrEditForm('add',this.parentMenu.node.id);" />
                    </Listeners>
                </ext:MenuItem>
                <ext:MenuItem Text="Chỉnh sửa" Icon="TableEdit">
                    <Listeners>
                        <Click Handler="loadAddOrEditForm('edit',this.parentMenu.node.id);" />
                    </Listeners>
                </ext:MenuItem>
                <ext:MenuSeparator>
                </ext:MenuSeparator>
                <ext:MenuItem Icon="SortAscending" Text="Sắp xếp">
                    <Listeners>
                        <Click Handler="loadOrderForm(this.parentMenu.node.id)" />
                    </Listeners>
                </ext:MenuItem>
                <ext:MenuSeparator>
                </ext:MenuSeparator>
                <ext:MenuItem Text="Xóa" Icon="Decline">
                    <Listeners>
                        <Click Handler="confirm(this.parentMenu.node.id,this.parentMenu.node.text);" />
                    </Listeners>
                </ext:MenuItem>
            </Items>
        </ext:Menu>
        <ext:Viewport ID="ViewPort2" runat="server">
            <Items>
                <ext:BorderLayout ID="BorderLayout1" runat="server">
                    <Center>
                        <ext:TreeGrid ID="treeCategory" runat="server" Border="false" BodyBorder="false"
                            AutoExpandColumn="Name" AllowLeafDrop="true" ContainerScroll="true" EnableDD="true"
                            RootVisible="false" Layout="Fit">
                            <DropConfig>
                                <CustomConfig>
                                    <ext:ConfigItem Name="getDropPoint" Value="getDropPoint" Mode="Raw" />
                                </CustomConfig>
                            </DropConfig>
                            <Columns>
                                <ext:TreeGridColumn Header="Tiêu đề" DataIndex="Name" Width="200">
                                </ext:TreeGridColumn>
                                <ext:TreeGridColumn Header="Đường dẫn" DataIndex="Url" Width="180" />
                                <ext:TreeGridColumn Header="Thứ tự" DataIndex="Order" Width="80" Align="Center" />
                                <ext:TreeGridColumn Header="Thể hiện" DataIndex="IsActive" Width="80" />
                                <ext:TreeGridColumn Header="Tin trang chủ" DataIndex="IsOnHomePage" Width="100" />
                                <ext:TreeGridColumn Header="Trên cùng" DataIndex="IsTop" Width="80" />
                                <ext:TreeGridColumn Header="Dưới cùng" DataIndex="IsBottom" Width="80" />
                                <ext:TreeGridColumn Header="Bên trái" DataIndex="IsLeft" Width="80" />
                                <ext:TreeGridColumn Header="Bên phải" DataIndex="IsRight" Width="80" />
                            </Columns>
                            <Sorter Property="Order" Direction="ASC">
                            </Sorter>
                            <Root>
                                <ext:AsyncTreeNode NodeID="0" Text="Root" />
                            </Root>
                            <Listeners>
                                <ContextMenu Handler="ContextMenu(node,e);" />
                                <MoveNode Fn="moveNode" />
                            </Listeners>
                            <BottomBar>
                                <ext:StatusBar ID="statusBar" Text="Danh mục menu tin bài">
                                </ext:StatusBar>
                            </BottomBar>
                        </ext:TreeGrid>
                    </Center>
                </ext:BorderLayout>
            </Items>
        </ext:Viewport>
    </form>
</body>
</html>
